<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\controller;

/**
 * 广告管理控制器
 */
class Advertisement extends AdminBase
{
    

    
    /**
     * 广告列表
     */
    public function advertisementList()
    {
        
        $where = $this->logicAdvertisement->getWhere($this->param);

        $list = $this->logicAdvertisement->getAdvertisementList($where, true, 'a.sort DESC, a.id DESC', DB_LIST_ROWS);
        
        $this->assign('list', $list);
        
        $this->assign('position_options', $this->logicAdvertisement->getPositionOptions());
        $this->assign('status_options', $this->logicAdvertisement->getStatusOptions());
        
        return $this->fetch('advertisement_list');
    }
    
    /**
     * 广告添加
     */
    public function advertisementAdd()
    {
        
        $this->advertisementCommon();
        
        return $this->fetch('advertisement_edit');
    }
    
    /**
     * 广告编辑
     */
    public function advertisementEdit()
    {
        
        $this->advertisementCommon();
        
        $id = $this->param['id'] ?? 0;
        
        if ($id > 0) {
            $info = $this->logicAdvertisement->getAdvertisementInfo(['id' => $id]);
            $this->assign('info', $info);
        }
        
        return $this->fetch('advertisement_edit');
    }
    
    /**
     * 广告添加与编辑通用方法
     */
    public function advertisementCommon()
    {
        
        IS_POST && $this->jump($this->logicAdvertisement->advertisementEdit($this->param));
        
        $this->assign('position_options', $this->logicAdvertisement->getPositionOptions());
        $this->assign('status_options', $this->logicAdvertisement->getStatusOptions());
    }
    
    /**
     * 广告删除
     */
    public function advertisementDelete()
    {
        
        $id = (int)($this->param['id'] ?? 0);
        
        $this->jump($this->logicAdvertisement->advertisementDel(['id' => $id]));
    }
    
    /**
     * 广告状态设置
     */
    public function setStatus()
    {
        
        $this->jump($this->logicAdvertisement->setAdvertisementStatus($this->param));
    }
    
    /**
     * 广告排序设置
     */
    public function setSortOrder()
    {
        
        $this->jump($this->logicAdvertisement->setAdvertisementSortOrder($this->param));
    }
    
    /**
     * 广告统计
     */
    public function advertisementStatistics()
    {
        
        $id = (int)($this->param['id'] ?? 0);
        
        if ($id <= 0) {
            $this->jump([RESULT_ERROR, '参数错误']);
        }
        
        $info = $this->logicAdvertisement->getAdvertisementInfo(['id' => $id]);
        if (empty($info)) {
            $this->jump([RESULT_ERROR, '广告不存在']);
        }
        
        $statistics = $this->logicAdvertisement->getAdvertisementStatistics(['advertisement_id' => $id]);
        
        $this->assign('info', $info);
        $this->assign('statistics', $statistics);
        
        return $this->fetch('advertisement_statistics');
    }
    
    /**
     * 导出广告列表
     */
    public function exportAdvertisementList()
    {
        
        $where = $this->logicAdvertisement->getWhere($this->param);
        
        $this->logicAdvertisement->exportAdvertisementList($where);
    }
}